package com.hoshiicloud.user.service.impl.company;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hoshiicloud.common.exception.CommonError;
import com.hoshiicloud.common.exception.ServiceException;
import com.hoshiicloud.user.entity.organization.ComOrganization;
import com.hoshiicloud.user.feign.IdGeneratorService;
import com.hoshiicloud.user.mapper.company.ICompanyLoginMapper;
import com.hoshiicloud.user.service.company.ICompanyLoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;


/**
 * <p>
 * 公司 服务实现类
 * </p>
 *
 * @author WGJ
 * @since 2019-04-20
 */
@Service
public class CompanyLoginServiceImpl extends ServiceImpl<ICompanyLoginMapper, ComOrganization> implements ICompanyLoginService {

    @Autowired
    private IdGeneratorService idGeneratorService;

//    @Autowired
//    INeo4jService neo4jService;

    /**
     * 采购商注册
     * TODO 事务
     * @param entity
     */
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "organization", allEntries = true)
    @Override
    public boolean registerCompany(ComOrganization entity) {
        String id = idGeneratorService.queryGeneratorId();

        if (id == null) {
            throw new ServiceException(CommonError.error("注册，获取全局id失败"));
        }
        entity.setId(Long.parseLong(id));
        /**
         *
         * TODO 查询平台/部门/支付id封装到list结合中
         */
        /**
         * TODO 获取创建人
         */
        /**
         * TODO 获取税号票号 ？
         */
        /**
         * TODO 校验电话号码和邮箱 js 端做
         */
        /**
         * TODO 查询配置类型
         */
        entity.setCreatedTime(LocalDateTime.now());
        entity.setModifiedTime(LocalDateTime.now());

        int row = baseMapper.insert(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("注册，保存注册信息失败"));
        }
        /**
         * 先写入neo4j数据库
         */
        JSONObject json = generaNeo4jJson(entity, id);
        //System.out.println(json.toJSONString());
//        Response tenant = neo4jService.createOrganization(json.toString());
//        if(200 !=tenant.getCode()){
//            throw new ServiceException(CommonError.error("同步租户到neo4j数据库失败"));
//        }

        return true;
    }

    private JSONObject generaNeo4jJson(ComOrganization entity, String id) {
        JSONObject json = new JSONObject();
        // TODO 暂时写死
            json.put("label","好货严选");
        Map<String,Object> properties = new HashMap<String,Object>();
        properties.put("name",entity.getName());
        //properties.put("platId",entity.getPlatId());
        properties.put("departmentId",entity.getDepartmentId());
        properties.put("idCard",entity.getIdCard());
        Object toJSON = JSONObject.toJSON(properties);
        json.put("properties",toJSON);
        json.put("uuid",id);
        return json;
    }

    /**
     * 采购商登陆
     *
     * @param userName
     * @param password
     * @return
     */
    @Override
    public boolean companyLogin(String userName, String password) {
        boolean result = false;
        //TODO 是否还有别的校验，登陆数据先放在缓存里
        ComOrganization comOrganizations = baseMapper.companyLogin(userName, password);
        if (null != comOrganizations) {
            result = true;
        }
        return result;
    }


    @Override
    public boolean companyLoginByPhoneNo(String phoneNo, String securityCode) {
        //TODO 等业务需求
        return false;
    }
}
